//
//  SecurityCenterViewModel.m
//  AIWAYS
//
//  Created by Navinfo on 2018/7/31.
//  Copyright © 2018年 wlq. All rights reserved.
//


#import "SecurityCenterViewModel.h"
#import "AppContext.h"
#import "UserDefaultsKeyDefine.h"
#import "NILoginService.h"
//#import "NIRemoteService.h"
#import "WeCloudManager.h"
#import "NIVehicleDataManager.h"

@implementation SecurityCenterViewModel

- (void)saveGestureLock:(NSString *)securityCodeSting{
    NIVehicleDataManager *manager = [[NIVehicleDataManager alloc] init];
    [manager saveGesturePwd:securityCodeSting userId:[[AppContext getInstance] userObject].userId vin:[[AppContext getInstance] vehicleObject].vin];
    [manager saveVerifyGesturePwd:@"1" userId:[[AppContext getInstance] userObject].userId vin:[[AppContext getInstance] vehicleObject].vin];
    
    [AppContext getInstance].vehicleObject.gesturePwd = securityCodeSting;
    [AppContext getInstance].vehicleObject.verifyGesturePwd = @"1";
}

- (void)removeGestureLock{
    NIVehicleDataManager *manager = [[NIVehicleDataManager alloc] init];
    [manager saveGesturePwd:@"" userId:[[AppContext getInstance] userObject].userId vin:[[AppContext getInstance] vehicleObject].vin];
    [manager saveVerifyGesturePwd:@"0" userId:[[AppContext getInstance] userObject].userId vin:[[AppContext getInstance] vehicleObject].vin];
    [AppContext getInstance].vehicleObject.gesturePwd = @"";
    [AppContext getInstance].vehicleObject.verifyGesturePwd = @"0";
}

- (NSString *)getGestureLockString{
    return [AppContext getInstance].vehicleObject.gesturePwd;
}

- (BOOL)isGestureLockSetup{
    if ([AppContext getInstance].vehicleObject.verifyGesturePwd) {
        return [AppContext getInstance].vehicleObject.verifyGesturePwd.boolValue;
    }
    return NO;
}


- (BOOL)isSecurityPasswordSetUp{
    if ([AppContext getInstance].vehicleObject.securityState && [[AppContext getInstance].vehicleObject.securityState boolValue]) {
        return YES;
    }
    return NO;
}


- (void)removeBiometricsLock{
    NIVehicleDataManager *manager = [[NIVehicleDataManager alloc] init];
    [manager saveVerifyFingerPwd:@"0" userId:[[AppContext getInstance] userObject].userId vin:[[AppContext getInstance] vehicleObject].vin];
    
    [AppContext getInstance].vehicleObject.verifyFingerPwd = @"0";
}

- (void)addBiometricsLock{
    NIVehicleDataManager *manager = [[NIVehicleDataManager alloc] init];
    [manager saveVerifyFingerPwd:@"1" userId:[[AppContext getInstance] userObject].userId vin:[[AppContext getInstance] vehicleObject].vin];
    
    [AppContext getInstance].vehicleObject.verifyFingerPwd = @"1";
}

- (BOOL)isBiometricsLockSetup{
    if ([AppContext getInstance].vehicleObject.verifyFingerPwd && [[AppContext getInstance].vehicleObject.verifyFingerPwd boolValue]) {
        return YES;
    }
    return NO;
}

- (void)setValidateSecurityWay:(ValidateSecurityWay)way{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInteger:way] forKey:[NSString stringWithFormat:@"%@_%@",VALIDATESECURITYWAY,[[AppContext getInstance] userObject].mobile]];
}

- (ValidateSecurityWay)getValidateSecurityWay{
    return [[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@_%@",VALIDATESECURITYWAY,[[AppContext getInstance] userObject].mobile]] integerValue];
}

- (BOOL)needValidateSecurityCode{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:LASTCONTROLTIMESTAMP]) {
        long long now = [[NSDate date] timeIntervalSince1970];
        long long last = [[[NSUserDefaults standardUserDefaults] objectForKey:LASTCONTROLTIMESTAMP] longLongValue];
        if ((now - last)/60 < 5) {
            return NO;
        }
    }
    return YES;
}

- (void)setupLastControlTimeStamp{
    if (![self needValidateSecurityCode] && [[NSUserDefaults standardUserDefaults] objectForKey:LASTCONTROLTIMESTAMP]) {
        return;
    }
    NSString *timeStamp = [NSString stringWithFormat:@"%lld",(long long)[[NSDate date] timeIntervalSince1970]];
    [[NSUserDefaults standardUserDefaults] setObject:timeStamp forKey:LASTCONTROLTIMESTAMP];
}

- (void)removeLastControlTimeStamp{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:LASTCONTROLTIMESTAMP];
}

- (void)checkSecurityCode:(NSString *)securityCode{
//    [[[NIRemoteService alloc] init] checkSecurityCode:securityCode vin:[AppContext getInstance].vehicleObject.vin userId:[AppContext getInstance].userObject.userId success:^(NIResultObject *result) {
//        self.checkSecurityErrorCode = result.errorCode;
//        self.checkSecurityResultCode = [NSNumber numberWithInteger:NIRequestStatusSuccess];
//    } failure:^(NIResultObject *result) {
//        self.checkSecurityErrorMessage = result.errorMessage;
//        self.checkSecurityErrorCode = result.errorCode;
//        self.checkSecurityResultCode = [NSNumber numberWithInteger:NIRequestStatusFailed];
//    }];
}

-(void)logout
{
    [self removeLastControlTimeStamp];
    
    WeCloudManager *manager = [WeCloudManager shareInstance];
//    [[NILoginService new]logout:manager.token userId:[AppContext getInstance].userObject.userId success:^(NIResultObject *result) {
//
//    } failure:^(NIResultObject *result) {
//        
//    }];
}

@end
